<?php

namespace app\api\controller;

use app\api\model\Category;
use app\api\model\CertificateCollect;
use app\api\model\Certificate as CertificateM;
use app\api\model\ManystoreShop;
use app\api\model\UserSearch;
use app\common\controller\Api;

class Certificate extends Api
{
    protected $noNeedLogin = ['ListData'];
    protected $noNeedRight = ['*'];
    public function ListData()
    {
        $param = $this->request->param();
        $Certificate = new CertificateM();
        $Category = new Category();
        $UserSearch = new UserSearch();
        $user= $this->auth;
        if(!empty($param['keywords'])){
            //搜索了 记得添加 历史记录
            $where['title|assistant_title']= ['like', '%' . $param['keywords'] . '%'];

            $SearchData['user_id']= $user->id;
            $SearchData['content']= $param['keywords'];
            $UserSearch->where($SearchData)->delete();
            $UserSearch->save($SearchData);
        }
        if(!empty($param['is_suggest'])){
            $where['is_suggest']=$param['is_suggest'];
        }
        if(!empty($param['c_id'])){
            $where['category_id']=$param['c_id'];
        }
        if(!empty($param['shop_id'])){
            $where['shop_id']=$param['shop_id'];
        }
        if(!empty($param['type'])){
            if(empty($param['c_id'])){
                switch($param['type']){
                    case 1:
                        $where_c['type'] = 'food';
                        break;
                    case 2:
                        $where_c['type'] = 'vote';
                        break;
                    case 3:
                        $where_c['type'] = 'drink';
                        break;
                    case 4:
                        $where_c['type'] = 'play';
                        break;
                    default:
                        $where_c['type'] = $param['type'];
                }
                $where_c['status']='normal';
                $where_c['pid']=0;
                $c_list= $Category->where($where_c)->column('id');
                $where['category_id']=['in',$c_list];
            }
        }
        $where['start_time']=['<',time()];
        $where['end_time']=['>',time()];
        $where['status']='1';
        $where['is_del']=0;
        $where['show_switch']=1;
        $list=$Certificate->where($where)->order('weigh desc')->paginate(10);
        $this->success('',$list);
    }

    public function details()
    {
        $user= $this->auth;
        $Certificate = new CertificateM();
        $CertificateCollect = new CertificateCollect();
        $ManystoreShop = new ManystoreShop();
        $param = $this->request->param();
        $where['id'] = $param['id'];
        $find=$Certificate->where($where)->find();
        $is_collect=0;
        $where_Collect['user_id'] = $user->id;
        $where_Collect['certificate_id']=$param['id'];
        $find_collect=$CertificateCollect->where($where_Collect)->find();
        if(!empty($find_collect)){
            $is_collect=1;
        }
        $find['is_collect']=$is_collect;
        $shop_data= $ManystoreShop->where(['id'=>$find['shop_id']])->field('id,name,logo,address_city,address,address_detail,longitude,latitude,tel')->find();
        $find['shop']=$shop_data;
        $this->success($find);
    }

    /**
     * 收藏/取消收藏
     * @return void
     */
    public function collect()
    {
        $user= $this->auth;
        $CertificateCollect = new CertificateCollect();
        $param = $this->request->param();
        $where['user_id'] = $user->id;
        $where['certificate_id']=$param['id'];
        $find=$CertificateCollect->where($where)->find();
        if(empty($find)){
            $data['user_id']=$user->id;
            $data['certificate_id']=$param['id'];
            $CertificateCollect->save($data);
            $this->success('收藏成功');
        }else{
            $CertificateCollect->where($where)->delete();
            $this->success('取消收藏成功');
        }
    }
}
